Skip to content

[Tracker] Use ResizeObserver instead of polling for document height#6174

Merged
ukutaht merged 13 commits intomasterfrom
use-resizeobserver
May 5, 2026
Merged

[Tracker] Use ResizeObserver instead of polling for document height#6174
ukutaht merged 13 commits intomasterfrom
use-resizeobserver

Conversation

@ukutaht
Copy link
Copy Markdown
Contributor

@ukutaht ukutaht commented Mar 18, 2026

Changes

Replaces document height polling with ResizeObserver to avoid forced reflows.

Fixes #5727

ResizeObserver is a fairly modern API. Should be OK for the main script since overall it doesn't have worse support than fetch-with-keepalive. The only gap I was able to find is Edge versions less than 79 do not support ResizeObserver while fetch support goes back longer on Edge. So we will lose pre-2020 Edge versions but they have negligible usage worldwide. It would be good to have some kind of feature matrix for tracker script and check against caniuse data when we update it.

Instead of keeping the old code as fallback for compat code, I've disabled engagement tracking completely in compat mode. Without fetch-with-keepalive engagement data isn't reliable anyways. But it should probably be checked with sites that actually use the compat mode.

Disabling engagements in compat mode also allowed me to remove some fallbacks in getCurrentScrollDepthPx function which are not relevant in modern browsers (window.scrollY and window.innerHeight are way better supported than fetch and ResizeObserver, the fallbacks were only relevant for IE).

Verification

On a local page with plausible installed, I profiled a scripted journey where page is loaded and scrolled after waiting a half a second. The old code produced 27 document reflows, with this PR there were 3 reflows. None of the remaining 3 were triggered by our script, they were all internal chrome events.

Comment thread tracker/src/engagement.js
Comment thread tracker/src/engagement.js
Comment thread tracker/src/engagement.js
function getCurrentScrollDepthPx() {
var body = document.body || {}
var el = document.documentElement || {}
var viewportHeight = window.innerHeight || el.clientHeight || 0
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing IE support for engagement tracking, these fallbacks are not necessary since window.scrollY and window.innerHeight are well-supported:

https://caniuse.com/mdn-api_window_innerheight
https://caniuse.com/mdn-api_window_scrolly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion, non-blocking: Can we keep 0 fallback? It doesn't cost much weight-wise and then the math won't crash entirely if for some reason these values are not truthy. Positive numbers are truthy.

@ukutaht ukutaht requested a review from RobertJoonas March 18, 2026 13:31
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 18, 2026

Analyzed 1026 tracker script variants for size changes.
The following tables summarize the results, with comparison with the baseline version in parentheses.

Main variants:

Brotli Gzip Uncompressed
npm_package/plausible.js (new variant) 2360B 2695B 6448B
plausible-web.js 2178B (-37B / -1.7%) 2498B (-39B / -1.5%) 5940B (-150B / -2.5%)

Important legacy variants:

Brotli Gzip Uncompressed
plausible.js 1119B (-27B / -2.4%) 1303B (-37B / -2.8%) 2855B (-150B / -5%)
plausible.hash.js 1091B (-24B / -2.2%) 1266B (-39B / -3%) 2734B (-150B / -5.2%)
plausible.pageview-props.tagged-events.js 1663B (-24B / -1.4%) 1932B (-33B / -1.7%) 4386B (-150B / -3.3%)
plausible.file-downloads.hash.pageview-props.revenue.js 1514B (-28B / -1.8%) 1787B (-38B / -2.1%) 3782B (-150B / -3.8%)
plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js 2299B (+16B / +0.7%) 2714B (+13B / +0.5%) 6192B (+18B / +0.3%)

Summary:

Brotli Gzip Uncompressed
Largest variant (plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js) 2299B (+16B / +0.7%) 2714B (+13B / +0.5%) 6192B (+18B / +0.3%)
Max change (plausible.compat.hash.local.manual.js) 958B (+27B / +2.9%) 1157B (+12B / +1%) 2347B (+18B / +0.8%)
Min change (plausible.local.manual.js) 867B (-43B / -4.7%) 1037B (-41B / -3.8%) 2081B (-150B / -6.7%)
Median change 1641B (-11B / -0.6%) 1942B (-32B / -1.4%) 4214B (-150B / -2.5%)

In total, 511 variants brotli size increased and 513 variants brotli size decreased.

ukutaht added 6 commits May 5, 2026 14:04
window.scrollY and window.innerHeight are compositor-cached values
that never trigger layout recalculation, unlike el.scrollTop and
el.clientHeight. The fallbacks only existed for IE which is already
unsupported.
@ukutaht ukutaht force-pushed the use-resizeobserver branch from 0e24d26 to d883d1d Compare May 5, 2026 12:07
@ukutaht ukutaht added tracker-release: minor Releases a new version of the tracker NPM package and removed tracker-release: minor Releases a new version of the tracker NPM package labels May 5, 2026
@ukutaht ukutaht added tracker-release: patch Releases a new version of the tracker NPM package and removed tracker-release: minor Releases a new version of the tracker NPM package labels May 5, 2026
@ukutaht ukutaht added this pull request to the merge queue May 5, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 5, 2026
@ukutaht ukutaht added this pull request to the merge queue May 5, 2026
Merged via the queue into master with commit 49448f5 May 5, 2026
32 checks passed
@ukutaht ukutaht deleted the use-resizeobserver branch May 5, 2026 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tracker-release: patch Releases a new version of the tracker NPM package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking script causes forced reflow of the page

2 participants